package com.dance.spring.learn.jdbc.dao.impl;

import com.dance.spring.learn.jdbc.dao.BookDao;
import com.dance.spring.learn.jdbc.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class BookDaoImpl implements BookDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(User user) {
        int update = jdbcTemplate.update("insert into user values(?,?,?)", user.getUserId(), user.getUserName(), user.getUstatus());
        if(update > 0){
            System.out.println("新增成功");
        }else{
            System.out.println("新增失败");
        }
    }

    @Override
    public void update(User user) {
        int update = jdbcTemplate.update("update user set username = ?,ustatus = ? where user_id = ?", user.getUserName(), user.getUstatus(), user.getUserId());
        if(update > 0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }

    @Override
    public void delete(User user) {
        int update = jdbcTemplate.update("delete from user where user_id = ?", user.getUserId());
        if(update > 0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

    @Override
    public int selectCount() {
        return jdbcTemplate.queryForObject("select count(1) from user",Integer.class);
    }

    @Override
    public User selectUserById(int id) {
        return jdbcTemplate.queryForObject("select * from user where user_id = ?", new BeanPropertyRowMapper<>(User.class),id);
    }

    @Override
    public List<User> selectUserList() {
        return jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
    }

    @Override
    public void batchAdd(List<User> userList) {
        List<Object[]> collect = userList.stream().map(x -> new Object[]{x.getUserId(), x.getUserName(), x.getUstatus()}).collect(Collectors.toList());
        int[] ints = jdbcTemplate.batchUpdate("insert into user values(?,?,?)", collect);
        System.out.println(Arrays.toString(ints));
    }

    @Override
    public void batchUpdate(List<User> userList) {
        List<Object[]> collect = userList.stream().map(x -> new Object[]{x.getUserName(), x.getUstatus(), x.getUserId()}).collect(Collectors.toList());
        int[] ints = jdbcTemplate.batchUpdate("update user set username=?,ustatus=? where user_id=?", collect);
        System.out.println(Arrays.toString(ints));
    }

    @Override
    public void batchDelete(List<User> userList) {
        List<Object[]> collect = userList.stream().map(x -> new Object[]{x.getUserId()}).collect(Collectors.toList());
        int[] ints = jdbcTemplate.batchUpdate("delete from user where user_id=?", collect);
        System.out.println(Arrays.toString(ints));
    }
}
